package com.hsgene.supplier.controller;

import com.hsgene.common.RangeType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.supplier.condition.AgencyCondition;
import com.hsgene.supplier.domain.GeneticTestingAgency;
import com.hsgene.supplier.dto.GeneticTestingAgencyDto;
import com.hsgene.supplier.service.AgencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 机构管理
 * @ProjectName: hdas-geneshop
 * @Package: com.hsgene.supplier.controller
 * @Author: FX
 * @CreateDate: 2018/10/17 11:00
 * @Version: 1.0
 * Copyright: Copyright (c) 2018
 */
@RequestMapping("/v1.0/genetic/testing/agency")
@RestController
public class AgencyController {
    @Autowired
    private AgencyService agencyService;

    /**
     * 添加检测机构
     *
     * @return
     * @author FX
     * @date 2018/10/17 11:07
     * @Param geneticTestingAgency
     */
    @PostMapping
    public ApiResult saveAgency(@RequestBody GeneticTestingAgency geneticTestingAgency) {
        return agencyService.saveAgency(geneticTestingAgency);
    }

    /**
     * 修改
     *
     * @param geneticTestingAgency
     * @return
     * @author FX
     * @date 2018/10/17 11:58
     */
    @PutMapping
    public ApiResult updateAgency(@RequestBody GeneticTestingAgency geneticTestingAgency) {
        return agencyService.updateAgency(geneticTestingAgency);
    }


    /**
     * 列表
     *
     * @param orderBy
     * @param isAsc
     * @param
     * @param
     * @param name
     * @param
     * @return
     * @author FX
     * @date 2018/10/17 13:50
     */
    @GetMapping
    public ApiResult getList(
            @RequestParam(name = "orderBy", defaultValue = "upate_date_time") String orderBy,
            @RequestParam(name = "isAsc", defaultValue = "false") Boolean isAsc,
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "range_type", required = false) String rangeType,
            @RequestParam(name = "isInvoice", required = false) Integer isInvoice) {
        if (rangeType != null && RangeType.ALL.getMessage().equals(rangeType)) {
            return agencyService.getAll();
        } else {
            AgencyCondition agencyCondition = new AgencyCondition();
            agencyCondition.setName(name);
            agencyCondition.setIsInvoice(isInvoice);
            agencyCondition.setLimitHelper(LimitHelper.create(page, size));
            agencyCondition.setOrderHelper(OrderHelper.create(orderBy, isAsc));
            return agencyService.getList(agencyCondition);
        }
    }

    /**
     * 详情
     *
     * @param id
     * @return
     * @author FX
     * @date 2018/10/17 11:58
     */
    @GetMapping("{id}")
    public ApiResult<GeneticTestingAgencyDto> getAgencyById(@PathVariable("id") String id) {
        return agencyService.getAgencyById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @author FX
     * @date 2018/10/17 14:31
     */
    @DeleteMapping("{id}")
    public ApiResult deleteAgenecy(@PathVariable("id") String id) {
        return agencyService.deleteAgenecy(id);
    }

    /**
     * 批量删除
     *
     * @param id
     * @return
     * @author FX
     * @date 2018/10/17 14:31
     */
    @DeleteMapping
    public ApiResult deleteAgenecyMore(@RequestBody List<String> id) {
        return agencyService.deleteAgencyMore(id);
    }

    /***
     * 批量查询机构信息
     * @author jcl
     * @date 2018/11/13 14:28
     * @param ids
     * @return
     */
    @GetMapping("/ids")
    public ApiResult<List<GeneticTestingAgency>> getAgencyByIds(@RequestParam("ids") String ids) {
        return agencyService.getAgencyByIds(ids);
    }

    /**
     * 重置机构对应管理员密码
     *
     * @param id 机构id
     * @return
     */
    @PutMapping("{id}/pwd")
    public ApiResult<NullObject> initAgencyPwd(@PathVariable("id") String id) {
        return agencyService.initAgencyPwd(id);
    }
}
